package com.hfxt.config;

import com.hfxt.utils.JwtUtil;
import com.hfxt.utils.Result;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

//如果用户从登录访问的话这个过滤器是需执行的  用来生成token
//如果用户访问addContact findConcatct delContact 不需要经过 并执行FilterConfig解析token
@Component
public class FilterConfig2 extends ZuulFilter {

    @Value("${common.login.url}")
    private String loginUrl;
    @Resource
    private JwtUtil jwtUtil;

    @Override
    public String filterType() {//进路由前进行拦截
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {//该过滤器是否被启动
        RequestContext requestContext=RequestContext.getCurrentContext();
        ///api/login/userLogin
        String url=requestContext.getRequest().getRequestURI();
//        List<String> excludeList= Arrays.asList(loginUrl.split(","));
//        return !excludeList.contains(url);
        for (String u : loginUrl.split(",")) {
            if (PathUtil.isPathMath(url,u)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {//具体执行逻辑
        RequestContext requestContext=RequestContext.getCurrentContext();
        HttpServletRequest request=requestContext.getRequest();
        try {
            //跳转到登录登录成功登录的用户对象就是Securit认证对象

            String name=request.getParameter("username");
            String pwd=request.getParameter("password");
            String token="";
            if (name.equals("lili")&&pwd.equals("123456")) {
                token=jwtUtil.createJWT(name,name,"admin");
            }else if (name.equals("mimi")&&pwd.equals("123456")){
                token=jwtUtil.createJWT(name,name,"user");
            }
            requestContext.setSendZuulResponse(true);
            requestContext.setResponseStatusCode(200);
            requestContext.addZuulResponseHeader("token",token);
            requestContext.setResponseBody("生成token成功");
        }catch (Exception e){
            requestContext.setSendZuulResponse(false);
            responseBody(requestContext,500,"生成token失败");
        }
        return null;
    }

    private void responseBody(RequestContext context,int code,String message){
        HttpServletRequest request=context.getRequest();
        Result result=new Result();
        result.setCode(code);
        result.setMsg(message);
        context.getResponse().setContentType("text/html;charset=utf-8");
        context.setResponseBody(result.toString());

    }

}
